package music.play.util;

import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import music.play.config.FileInfo;
import music.play.config.JsonResult;
import music.play.config.PlayerEnum;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AudioUtil {

    private static final Log log = Log.getLog(AudioUtil.class);

    private static final String MusicPlayer = PropKit.get("musicPlayer");

    private AudioUtil() {

    }


    //循环播放参数(-q 不输出文件信息)
    private static String LoopParam() {
        PlayerEnum playerEnum = PlayerEnum.getBy(MusicPlayer);
        if (null == playerEnum)
            return "";
        switch (playerEnum) {
            case MadPlay:
                return " -r -q ";// -r循环播放一次
            case Mpg123:
                return " -z -q "; //--loop -1:循环播放一个(正数播放次数) -z 随机播放
            default:
                return "";
        }
    }

    /**
     * 获取音乐路径
     */
    public static String AudioPath() {
        String path;
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows")) {
            path = "d:\\audio";
        } else {
            path = "/mnt/audio";
        }

        File file = new File(path);
        if (!file.exists())
            System.out.println("创建目录:" + file.mkdirs());
        return path;
    }

    /**
     * 读取语音文件列表
     */
    public static JsonResult<List<FileInfo>> AudioList() {
        JsonResult<List<FileInfo>> result = new JsonResult<>();
        String path = AudioPath();
        // 创建File对象
        File directory = new File(path);

        // 确保目录存在且确实是一个目录
        if (directory.exists() && directory.isDirectory()) {
            // 获取目录下所有文件和目录
            File[] files = directory.listFiles();
            if (null != files && files.length > 0) {
                List<FileInfo> list = new ArrayList<>();
                for (File file : files) {
                    list.add(new FileInfo(file.getName()));
                }
                result.setSuccess(true).setMessage("音乐列表").setData(list);
            } else {
                result.setSuccess(false).setMessage("目录下没有文件[" + path + "]");
            }
        } else {
            result.setSuccess(false).setMessage("目录不存在或不是一个目录[" + path + "]");
        }
        return result;
    }


    /**
     * 删除文件
     */
    public static JsonResult<String> AudioDel(String fileName) {
        if (null == fileName)
            return JsonResult.failed("删除失败,文件不能为空.");
        String path = AudioPath();
        File file = new File(path + File.separator + fileName);
        if (file.exists()) {
            if (file.delete()) {
                return JsonResult.success("删除成功.");
            } else {
                return JsonResult.failed("删除失败.");
            }
        } else {
            return JsonResult.failed("删除失败,文件不存在.");
        }
    }

    /**
     * 开始播放
     */
    public static void Play() {
        log.info(Stop());
        log.info("==>Play");
        JschUtil.ExecCommandByThread(MusicPlayer + LoopParam() + AudioUtil.AudioPath() + "/*.mp3");
    }

    /**
     * 停止播放
     */
    public static String Stop() {
        log.info("==>Stop");
        return JschUtil.ExecCommand("kill $(pidof " + MusicPlayer + ")");
    }
}